package com.batman.baselibrary.widget.update;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.batman.baselibrary.R;

import org.lzh.framework.updatepluginlib.base.CheckNotifier;
import org.lzh.framework.updatepluginlib.util.SafeDialogHandle;

/**
 * 作者：yuhb on 2019/7/12 17:42.
 * 描述：
 */
public class UpdateNotifier extends CheckNotifier {

    @Override
    public Dialog create(Activity activity) {
//        String updateContent = "版本号: " + update.getVersionName() + "\n\n\n"
//                + update.getUpdateContent();
//        AlertDialog.Builder builder = new AlertDialog.Builder(activity)
//                .setMessage(updateContent)
//                .setTitle("你有新版本需要更新")
//                .setPositiveButton("立即更新", new DialogInterface.OnClickListener() {
//                    @Override
//                    public void onClick(DialogInterface dialog, int which) {
//                        sendDownloadRequest();
//                        SafeDialogHandle.safeDismissDialog((Dialog) dialog);
//                    }
//                });
//        if (update.isIgnore() && !update.isForced()) {
//            builder.setNeutralButton("忽略此版本", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    sendUserIgnore();
//                    SafeDialogHandle.safeDismissDialog((Dialog) dialog);
//                }
//            });
//        }
//
//        if (!update.isForced()) {
//            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialog, int which) {
//                    sendUserCancel();
//                    SafeDialogHandle.safeDismissDialog((Dialog) dialog);
//                }
//            });
//        }
//        builder.setCancelable(false);
//        return builder.create();

        String updateContent = "版本号: " + update.getVersionName() + "\n\n\n"
                + update.getUpdateContent();

        View view = LayoutInflater.from(activity).inflate(R.layout.fragment_update_app, null);
        TextView textView = view.findViewById(R.id.tv_update_info);
        TextView tvCancel = view.findViewById(R.id.tv_cancel);
        TextView tvOk = view.findViewById(R.id.tv_ok);
        textView.setText(Html.fromHtml(update.getUpdateContent()));
        // 设置style 控制默认dialog带来的边距问题
        AlertDialog.Builder builder = new AlertDialog.Builder(activity, R.style.dialog_style);
        builder.setView(view);
        //是否是强制更新 true 强制更新
        if (!update.isForced()) {
            tvCancel.setVisibility(View.VISIBLE);
        } else {
            tvCancel.setVisibility(View.GONE);
        }

        builder.setCancelable(false);
        final AlertDialog dialog = builder.create();
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendDownloadRequest();
                SafeDialogHandle.safeDismissDialog((Dialog) dialog);
            }
        });
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendUserCancel();
                SafeDialogHandle.safeDismissDialog((Dialog) dialog);
            }
        });
        return dialog;


    }

}
